﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SerialForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool addData = false;
        	   
	        private void Main()
	        {
	           

	            SerialPort p = new SerialPort(Console.ReadLine());
	            p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
                
	            p.Open();
	            string line;
	            do
	            {
	                line = Console.ReadLine();
	                p.Write(line);
	            } while (line != "quit");
	            p.Close();
	        }

            public void addTxt(string txt)
            {
                if (this.txtRes.InvokeRequired)
                {
                    this.txtRes.BeginInvoke(
                        new MethodInvoker(
                        delegate() { addTxt(txt); }));
                }
                else
                {
                    this.txtRes.Text+= txt;//.Items.Clear();
                }
            }
            public void addLst(string txt)
            {    if (this.lstData.InvokeRequired)
                {
                    this.lstData.BeginInvoke(
                        new MethodInvoker(
                        delegate() { addLst(txt); }));
                }
                else
                {
                    this.lstData.Items.Insert(0,txt);//.Items.Clear();
                }
            }

            public void ClearLst()
            {
                if (this.lstData.InvokeRequired)
                {
                    this.lstData.BeginInvoke(
                        new MethodInvoker(
                        delegate() { ClearLst(); }));
                }
                else
                {
                    this.lstData.Items.Clear();//.Items.Clear();
                }
            }
	        private void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
	        {
                    string sr = (sender as SerialPort).ReadExisting();
                    if (sr.IndexOf(".") >= 0)
                    {
                        //int length = sr.IndexOf('\r') >= 0?sr.IndexOf('\r'):sr.Length - 1;
                        //length -= sr.IndexOf("0.");
                        string val = sr;//.Substring(sr.IndexOf("0."),length);
                        string[] valcol = val.Split('\r');
                          
                            foreach (string s in valcol)
                            {
                                string t = s.Trim();
                                t = s.Replace('\n',' ');
                                if (t.Trim() != "")
                                {
                                    try
                                    {
                                        addTxt(";" + t);
                                        addLst(t);
                                        if (addData)
                                        {
                                            using (StreamWriter wd = new StreamWriter(File.Open("sonarReadings.txt",FileMode.Append)))
                                            {
                                                wd.Write(t + "\r\n");
                                            }
                                        }
                                    }
                                    catch { }
                                }
                            }


                    }
                
	        }

            private void Form1_Load(object sender, EventArgs e)
            {
                string[] names = SerialPort.GetPortNames();

                foreach (string name in names)
                { lstPorts.Items.Add(name); }
            }

            private void btnConn_Click(object sender, EventArgs e)
            {
                if (lstPorts.SelectedIndex == -1)
                {lstPorts.SelectedIndex = 0;}
                SerialPort p = new SerialPort(lstPorts.Items[lstPorts.SelectedIndex] as string);
                p.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived);
                p.Close();
                p.Open();
                while (true)
                {
                    Application.DoEvents();
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                txtRes.Copy();
                textBox1.Text = txtRes.Text;
            }

            private void btnClear_Click(object sender, EventArgs e)
            {
                txtRes.Text = "";
                ClearLst();
            }
      
            private void btnControl_Click(object sender, EventArgs e)
            {
                if (!addData) //if we are not currently recording, start recording
                {
                    using (StreamWriter wd = new StreamWriter(File.Open("sonarReadings.txt", FileMode.Append)))
                    {
                        wd.WriteLine(txtNotes.Text);
                        wd.WriteLine("*******************************");
                    }
                }
                addData = !addData;
            }
    }
}
